import { Card, Button, Form, Input, message } from 'antd';
import "../index.less";
import { useNavigate, useParams } from 'react-router-dom';
import { useEffect } from 'react';

import {reqUpdataHospital, reqAddHospital, reqGetHospitalById } from "@api/hospital/hospitalSet"
import { AddHospitalParams } from "@api/hospital/model/hospitalSettypes"
export default function AddHospital() {
    /* const [hospital, setHospital] = useState({
        apiUrl:'',
        contactsName:'',
        contactsPhone:'',
        hoscode:'',
        hosname:'',
    }) */
    const [form] = Form.useForm();
    const params = useParams();
    const id = +(params.id as string)
    const getHospitalById = async () => {
        const hospital = await reqGetHospitalById(id)
        form.setFieldsValue(hospital)
    }
    useEffect(() => {
        if (!params.id) return;
        getHospitalById();
    }, [])
    const navigate = useNavigate()
    const onFinish = async (values: AddHospitalParams) => {
        if(id){
            await reqUpdataHospital({
                ...values,
                id,
            })
        }else{
            await reqAddHospital(values)
        }
        
        message.success(`${id? '修改' : '添加'}医院成功`)
        navigate('/syt/hospital/hospitalSet')
    }
    const goBack = () => {
        navigate('/syt/hospital/hospitalSet')
    }
    return (
        <Card>
            {/*"apiUrl": "string",
              "contactsName": "string",
              "contactsPhone": "string",
             "hoscode": "string",
             "hosname": "string", */}
            <Form
                form={form}
                labelCol={{ span: 2 }}
                wrapperCol={{ span: 22 }}
                onFinish={onFinish}
                // initialValues = {hospital}
            >
                <Form.Item
                    label="医院名称"
                    name="hosname"
                    rules={[{ required: true, message: '请输入医院名称' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="医院编号"
                    name="hoscode"
                    rules={[{ required: true, message: '请输入医院编号' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="api基础路径"
                    name="apiUrl"
                    rules={[{ required: true, message: '请输入api基础路径' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人姓名"
                    name="contactsName"
                    rules={[{ required: true, message: '请输入联系人姓名' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[{ required: true, pattern: /^1[3-9][0-9]{9}$/, message: '请输入合法的联系人手机' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item wrapperCol={{ offset: 2 }}>
                    <Button type="primary" htmlType="submit">
                        保存
                    </Button>
                    <Button className='ml' onClick={goBack}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    );
}